package toubiao.service.impl.init;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import toubiao.service.init.InitResourceServiceI;
import toubiao.service.init.InitRoleServiceI;
import toubiao.service.init.InitServiceI;
import toubiao.service.init.InitUserServiceI;

@Service
public class InitServiceImpl implements InitServiceI , ApplicationListener<ContextRefreshedEvent>{

	private Logger logger=LoggerFactory.getLogger(InitServiceImpl.class);
	
	//private int times=1;
	
	@Autowired
	private InitResourceServiceI initResourceService;
	@Autowired
	private InitRoleServiceI initRoleService;
	@Autowired
	private InitUserServiceI initUserService;
	
	@Override
	public void clearAndInitAll(){
		initResourceService.clearAndInit();
		initRoleService.clearAndInit();
		initUserService.clearAndInit();
	}
	
	@Override
	public void clearAll(){
		initResourceService.clearResource();
		initRoleService.clearRole();
		initUserService.clearUser();
	}
	
	
	@Override
	public void initAll(){
		initResourceService.init();
		initRoleService.init();
		initUserService.init();
	}
	    /* (非 Javadoc)
	    *  该方法会运行2次，第一是父容器加载完成，即 spring的监听器中设置的容易
	    *  2次：springMvc 子容器加载完成后运行
	    * 
	    * @param event
	    * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	    */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		//System.out.println("times ="+times++);
		// TODO Auto-generated method stub
		if(event.getApplicationContext().getParent() == null){//root application context 没有parent，他就是老大.  
	        //需要执行的逻辑代码，当spring容器初始化完成后就会执行该方法。  
			/* logger.info("in onApplicationEvent");*/
			//initAll();
	    }  
	}
}
